package cn.panshi.cdcdemo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;

@Configuration
public class MongoConfig {
	@Value("${mongodb.primary.uri}")
	private String primaryUri;
	@Value("${mongodb.secondary.uri}")
	private String secondaryUri;

	@Bean
	@Primary
	public MongoDatabaseFactory mongoDbFactory() {
		return new SimpleMongoClientDatabaseFactory(primaryUri);
	}

	@Bean
	@Primary
	public MongoTemplate mongoTemplate() {
		MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
		return mongoTemplate;
	}

	@Bean
	public MongoTemplate mongoTemplateSecondary() {
		return new MongoTemplate(new SimpleMongoClientDatabaseFactory(secondaryUri));
	}
}